package generatorGraphics.graphics;

import grammaticalClasses.nwConsole.ShowStatCur;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import softwareControl.SearchCommands;

public class Temperature implements GraphInterface{

    private SearchCommands search;    
    private String name;
    private String help;
        
    private ChartPanel chartPanel;
    private ArrayList<String> unitList;
    private ArrayList<ArrayList<String>> optionsList;
    private String unitActive;
    
    private ArrayList<Object> answer;
    private ArrayList<String> commands;
    
    public Temperature(){
        
        unitList = new ArrayList<>();
        optionsList = new ArrayList<>();
        
        name = "Temperature";
        help = "Help of Temperature";
        
        configUnitOptions();
        
        commands = new ArrayList<>();
        commands.add("ShowStatCur");
        
        //createGraph("Erro-Temperature-Erro");
    }
    
    public void start(String directory){
        
        if(directory == null || directory.equals("")){
            search = new SearchCommands(commands);
        } else{
            search = new SearchCommands(directory, commands);
        }
        answer = (ArrayList<Object>) search.getCommandsAnswer();
        
    }
    
    private void configUnitOptions(){

        // Adicionando as unidades
        //--Não existe definição de Opções
        
        //Adicionando as opções
        //--Não existe definição de Opções
    }
    
    private void createGraph(String unit){
        
        int indice;
        
//        if(search == null){
//            search = new SearchCommands(commands);
//            answer = (ArrayList<Object>) search.getCommandsAnswer();
//        }
        
        switch (unit){
            case "":                
                indice = search.getAnswer(commands, "ShowStatCur");
                chartPanel = new ChartPanel(buildGraph((ArrayList<ShowStatCur>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);
                
                break;
            default:
                
                chartPanel = new ChartPanel(null);
                break;
        }        
    }
    
    private JFreeChart buildGraph(ArrayList<ShowStatCur> auxCur){
    
        ShowStatCur aux = auxCur.get(0);
        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(int j=0; j< aux.getMoteStats().size(); j++){
            dataset.setValue(aux.getMoteStats().get(j).getT(), 
                "Temperatura", aux.getMoteStats().get(j).getId() + "Id");
        }
        JFreeChart chart = ChartFactory.createBarChart("Temperatura Atual nos ultimos " 
            + aux.getTime().toString() + " segundos",
            "Dispositivos", "Temperatura (ºC)", dataset, 
            PlotOrientation.VERTICAL, false, true, false);
        chart.setBackgroundPaint(new Color(238, 238, 238));

        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.black);
        
        return chart;
        
    }
    
    /**
     * Verifica se o 'texto' é um número. Podendo ser inteiro ou float.
     * @param texto
     * @return 
     */
    public static boolean isNumber(String texto) {  
        if(texto == null) {
            return false;
        }  
        for (char letra : texto.toCharArray()) {
            if((letra < '0' || letra > '9') && (letra != '.')) {
                return false;
            }
        }  
        return true;  
        
    }
    
    @Override
    public String toString(){
        return name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public ArrayList<String> getUnitList() {
        return unitList;
    }

    /**
     * Retorna o chartPanel 'default'
     * @return 
     */
    @Override
    public ChartPanel getChartPanel() {
        if(search != null){
            createGraph("");
            return chartPanel;
        } else {
            return new ChartPanel(null);
        }
    }

    @Override
    public String getHelp() {
        return help;
    }

    @Override
    public ChartPanel getChartPanel(String unit) {
        if(search != null){
            for(int i=0; i<unitList.size(); i++){
                if(unit.equals(unitList.get(i))){
                    createGraph(unit);
                }
            }
            return chartPanel;
        } else {
            return new ChartPanel(null);
        }
    }

    @Override
    public ArrayList<String> getOptionsList(String unit) {
        ArrayList<String> auxReturn = new ArrayList<>();
        
        for(int i=0; i<optionsList.size(); i++){
            if((optionsList.get(i)).get(0).equals("[UNIT]" + unit)){
                
                for(int j=1; j<optionsList.size(); j++){
                    auxReturn.add(optionsList.get(i).get(j));
                }
            }
        }
        return auxReturn;
    }
    
}
